package apt.core;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.FileUtils;

import apt.core.entity.PackageInfo;

public class PackageManager {
	private Configuration conf = new Configuration();

	public void install(String name, String version, boolean noUpgrade) {
		if (conf.getInstalledPackageMap().get(name) != null) {
			System.out.println(name + " already installed");
			return;
		}
		
		try {
			uncompressTarGZ(new File(conf.getLocalRepo() + "\\" + name + "-" + version + ".tar.gz"), 
					new File(conf.getInstallDir() + "\\" + name));
		} catch (IOException e) {
			System.out.println("fail");
			e.printStackTrace();
		}
		conf.getInstalledPackageMap().put(name, new PackageInfo());
	}
	
	public void remove(String name) {
		if (conf.getInstalledPackageMap().get(name) == null) {
			System.out.println(name + " does not exist");
			return;
		}
		try {
			FileUtils.deleteDirectory(new File(conf.getInstallDir() + "\\" + name));
		} catch (IOException e) {
			System.out.println("fail rem");
			e.printStackTrace();
		}
		conf.getInstalledPackageMap().remove(name);
	}
	
	protected void uncompressTarGZ(File tarFile, File dest) throws IOException {
	    dest.mkdir();
	    TarArchiveInputStream tarIn = null;

	    tarIn = new TarArchiveInputStream(
	                new GzipCompressorInputStream(
	                    new BufferedInputStream(
	                        new FileInputStream(
	                            tarFile
	                        )
	                    )
	                )
	            );

	    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
	    // tarIn is a TarArchiveInputStream
	    while (tarEntry != null) {// create a file with the same name as the tarEntry
	        File destPath = new File(dest, tarEntry.getName());
//	        System.out.println("working: " + destPath.getCanonicalPath());
	        if (tarEntry.isDirectory()) {
	            destPath.mkdirs();
	        } else {
	            destPath.createNewFile();
	            //byte [] btoRead = new byte[(int)tarEntry.getSize()];
	            byte [] btoRead = new byte[1024*1024];
	            //FileInputStream fin 
	            //  = new FileInputStream(destPath.getCanonicalPath());
	            BufferedOutputStream bout = 
	                new BufferedOutputStream(new FileOutputStream(destPath));
	            int len = 0;

	            while((len = tarIn.read(btoRead)) != -1)
	            {
	                bout.write(btoRead,0,len);
	            }

	            bout.close();
	            btoRead = null;

	        }
	        tarEntry = tarIn.getNextTarEntry();
	    }
	    tarIn.close();
	} 
}
